package com.enjoy;

import com.alibaba.druid.pool.DruidDataSource;
import com.enjoy.mapper.UserMapper;
import com.enjoy.pojo.User;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.List;

public class MybatisTest {

    Logger logger = LoggerFactory.getLogger("enjoy");


    /**
     * 使用默认配置
     */
    private SqlSession getSqlSessionFactory() {
        // 数据库连接池信息
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setUrl("jdbc:mysql://10.10.2.60:3306/enjoy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false");

//        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
//        dataSource.setUsername("BSHIP_CDR");
//        dataSource.setPassword("BSOFT");
//        dataSource.setUrl("jdbc:oracle:thin:@10.10.22.75:1521/hip32");
        // 采用 MyBatis 的 JDBC 事务方式
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        // 创建 Configuration 对象
        Configuration configuration = new Configuration(environment);
        // 加入一个映射器
        configuration.addMappers("com.enjoy.mapper");

        //使用 SqlSessionFactoryBuilder 构建 SqlSessionFactory
        return new SqlSessionFactoryBuilder().build(configuration).openSession();
    }

    /**
     * 使用配置文件
     */
    private SqlSession getSqlSessionFactoryWithConfigXml() {

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/mybatis.xml");
        //使用 SqlSessionFactoryBuilder 构建 SqlSessionFactory
        return new SqlSessionFactoryBuilder().build(inputStream).openSession();
    }

    @Test
    public void test1() {
        SqlSession sqlSession = getSqlSessionFactory();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(51);
        user.setName("ii");
        user.setAge(63);
//        int insert = userMapper.insert(user);
        List<User> list = userMapper.select();

        logger.info("结果:{}", list);
    }

    @Test
    public void test2() {
        SqlSession sqlSession = getSqlSessionFactoryWithConfigXml();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(66);
        user.setName("yy");
        user.setAge(63);
        int insert = userMapper.insert(user);
        sqlSession.commit();
        logger.info("结果:{}", insert);
    }

    @Test
    public void test3() {
        SqlSession sqlSession = getSqlSessionFactory();

        //会去这个包下面找xml
        List<Object> list= sqlSession.selectList("com.enjoy.mapper.UserMapper.select");


        logger.info("结果:{}", list);;
    }
}
